﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net.Http;
using System.Security.Claims;
using TomNet.App.Model.DB.Identity;
using TomNet.App.Model.DTO;
using TomNet.App.WebApi.Common;
using TomNet.AspNetCore.Data;
using TomNet.AspNetCore.Mvc;
using TomNet.Authentication.JwtBearer;
using TomNet.Caching;
using TomNet.Core;
using TomNet.Core.Options;
using TomNet.Data;
using TomNet.Extensions;
using TomNet.Security;
using TomNet.SqlSugarCore.Entity;
using TomNet.Security.Claims;
namespace TomNet.App.WebApi.Controllers
{
    [Authorize]
    public class APPController : ApiController
    {
        private readonly IUnitOfWork _unitOfWork;
        private IDistributedCache _cache;
        //private IHttpClientFactory _httpClientFactory;
        public APPController(IUnitOfWork unitOfWork, IServiceProvider provider)
        {
            _unitOfWork = unitOfWork;
            _cache = provider.GetService<IDistributedCache>();
            //_httpClientFactory = httpClientFactory;
        }
        /// <summary>
        /// Jwt登录
        /// </summary>
        /// <returns>JSON操作结果</returns>
        [AllowAnonymous]
        [HttpPost]
        public AjaxResult Login(TomNet.App.Model.DTO.APP.LoginDTO dto)
        {
            Check.NotNull(dto, nameof(dto));

            if (!ModelState.IsValid)
            {
                return new AjaxResult("提交信息验证失败", AjaxResultType.Error);
            }

            var login = _unitOfWork.DbContext.Queryable<UserLogin>().First(m => m.LoginName == dto.LoginName && m.Password == dto.LoginPass);
            if (login == null)
            {
                return new AjaxResult("账户或密码错误", AjaxResultType.Error);
            }
             var user = _unitOfWork.DbContext.Queryable<Users>().First(m => m.Id == login.UserID);
           //生成Token，这里只包含最基本信息，其他信息从在线用户缓存中获取
            Claim[] claims =
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                new Claim(ClaimTypes.Name, login.LoginName),
                new Claim(ClaimTypes.Role, "系统管理员"),
                new Claim("UserInfo", TomNet.Json.JsonHelper.ToJson(user)),
            };
            TomNetOptions options = HttpContext.RequestServices.GetTomNetOptions();
            string token = JwtHelper.CreateToken(claims, options);

            return new AjaxResult("登录成功", AjaxResultType.Success, token);
        }
        //[HttpPost]
        public AjaxResult aaabbbccc()
        {
            Users u = TomNet.Json.JsonHelper.FromJson<Users>(HttpContext.User.Identity.GetClaimValueFirstOrDefault("UserInfo"));

            return new AjaxResult("登录成功", AjaxResultType.Success, u);
        }

        [AllowAnonymous]
        public AjaxResult FreeApi()
        {
            var user = _unitOfWork.DbContext.Queryable<UserLogin>().ToList();
            return new AjaxResult("允许访问", AjaxResultType.Success, user);
        }

    }
}